#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
int score = 0; //分数
int level = 1; //等级
int speed = 150; //速度
int i;
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   //U8G2lib.h屏幕驱动初始
struct SNAKE                                            // 蛇的结构及初始化
{
int head_x ; //蛇头坐标
int head_y ;
int x[200];
int y[200]; 
int longth;  //蛇的长度
int dir;     //控制蛇的方向
int life;
}snake={16,20,{16,20,24},{20,20,20},3,UP,1};            
struct FOOD                                             //食物结构及初始化
{
    int x;
    int y;
    int eaten; //判断食物是否被吃
}food={24,40,true};
//按键扫描
void key(void){
if(LOW==digitalRead(4)){
    snake.dir = UP;       //向上
}
if(LOW==digitalRead(5)){
    snake.dir = DOWN;     //向下
}
if(LOW==digitalRead(6)){
    snake.dir = LEFT;     //向左
}
if(LOW==digitalRead(7)){
    snake.dir = RIGHT;    //向右
}
}
//绘制蛇or食物
void draw_snake(int x,int y){
u8g2.drawBox(x,y,4,4); //在xy坐标处绘制一个4x4的方块，作为蛇的基本单位
}
//初始ui
void UI()  {
u8g2.drawFrame(0,0,101,64);//外边界
u8g2.drawFrame(1,1,99,62);//内边界
u8g2.setFont(u8g2_font_5x7_tf); //设置字体
u8g2.drawStr(104,12,"LEVEL");  //显示字体
u8g2.drawStr(104,40,"SCORE");
}
//显示
void screen(void){ 
        for (i = 0; i < snake.longth;i++){ //绘制蛇身
        draw_snake(snake.x[i],snake.y[i]);
        }       
        drawscore(104,18,level); //绘制等级
        drawscore(104,46,score); //绘制分数
        draw_snake(food.x,food.y); //绘制食物

}
//生成食物
/* void draw_food(void){
    int food_out = 0;//食物是否在蛇体内
    if(food.eaten==true)
    {
        while (food_out == 0){
            food_out == 1;
            food.x = (uint8_t)(random(4, 100) / 4) * 4;
            food.y = (uint8_t)(random(4, 60) / 4) * 4;//生成随机坐标
            
            for(int y=snake.longth-1;i > 0;i--){ //判断
                if(food.x == snake.x[i] && food.y == snake.y[i])
                {
                  food_out=0;//循环
                }
            } 
        }
        
    }
    food.eaten = false;
}//这一段有点小BUG，可能会无限循环 故弃用*/
void draw_food(void){
    if(food.eaten==true)
    {
            food.x = (uint8_t)(random(4, 100) / 4) * 4;
            food.y = (uint8_t)(random(4, 60) / 4) * 4;//生成随机坐标
        }
        food.eaten = false; //更新食物状态
    }
//移动蛇
void move(){
switch (snake.dir) {
case UP:
        snake.head_y -=4;
    if (snake.head_y<=1){
        snake.head_y=60; 
    }break;
case DOWN:
        snake.head_y +=4;
    if (snake.head_y>=61){
        snake.head_y=0; 
    }break;
case LEFT:
        snake.head_x -=4;
    if (snake.head_x<=1){
        snake.head_x=96; 
    }break;
case RIGHT:
        snake.head_x +=4;
    if (snake.head_x>=97){
        snake.head_x=0; 
    }break;
    }
    if ((snake.head_x==food.x)&&(snake.head_y==food.y))
    {
        food.eaten = true;
        snake.longth++;
        score++;
        
        if (speed>=50)
        {
            speed=speed-5*score;
        }
        
        level = score/5 + 1;
    }
for (i =snake.longth-1;i>0;i--) 
{
    snake.x[i]=snake.x[i-1];
    snake.y[i]=snake.y[i-1];
}
 snake.x[0]=snake.head_x;
 snake.y[0]=snake.head_y;
}
//绘制分数
void drawscore(int x,int y,int data){
    u8g2.setFont(u8g2_font_5x7_tf);
    u8g2.setCursor(x, y);
    u8g2.print(data);
}
void setup(void){
    pinMode(4,INPUT);
    pinMode(5,INPUT);
    pinMode(6,INPUT);
    pinMode(7,INPUT);
    digitalWrite(4,HIGH);
    digitalWrite(5,HIGH);
    digitalWrite(6,HIGH);
    digitalWrite(7,HIGH);
    u8g2.begin(); //U8G2显示函数
    randomSeed(analogRead(3)); //初始随机数
}     
void loop(void){  
  
    key();
    move();
    draw_food(); //计算相关数据
    u8g2.firstPage(); //对数据进行显示
     do{ 
        UI();
        screen();
        } while (u8g2.nextPage());
         delay(speed); //分数与速度成正比
    
}